﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace XDA_ROM_Hub
{
    public partial class Tweaks : Form
    {
        private delegate void updateText(string str);

        public Tweaks()
        {
            InitializeComponent();
            UserClosing = false;
            this.FormClosing += new FormClosingEventHandler(Form1_FormClosing);
        }

        public bool UserClosing { get; set; }

        void buttonExit_Click(object sender, EventArgs e)
        {
            UserClosing = true;
            cencelbtn();
        }

        void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            switch (e.CloseReason)
            {
                case CloseReason.ApplicationExitCall:
                    break;
                case CloseReason.FormOwnerClosing:
                    break;
                case CloseReason.MdiFormClosing:
                    break;
                case CloseReason.None:
                    break;
                case CloseReason.TaskManagerClosing:
                    break;
                case CloseReason.UserClosing:
                    if (UserClosing)
                    {
                        cencelbtn();
                    }
                    else
                    {
                        cencelbtn();
                    }
                    break;
                case CloseReason.WindowsShutDown:
                    break;
                default:
                    break;
            }
        }

        public void cencelbtn()
        {
            Properties.Settings.Default.Reset();
            if (File.Exists(Application.StartupPath + @"\build\build.prop"))
            {
                File.Delete(Application.StartupPath + @"\build\build.prop");
            }
            Application.Exit();

        }

        public void backbtn()
        {
            this.Hide();
        }

        private void Tweaks_Load(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            
            backbtn();
        }

        public void UNLCOK()
        {
            if (Properties.Settings.Default.UNLOCKCODE == "")
            {

            }
            else
            {
                try
                {
                    Process.Start(Application.StartupPath + @"\rebootFastboot.bat");

                    
                    // Process.Start(Application.StartupPath + @"\rebootSystem.bat");

                }
                finally
                {
                    string strCmdText;
                    strCmdText = "-i 0x0fce oem unlock 0x" + Properties.Settings.Default.UNLOCKCODE;
                    System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", strCmdText);

                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            bool EnableGuide = Properties.Settings.Default.EnableGuide;
            if (EnableGuide == true)
            {
                MessageBox.Show("This will unlock your bootloader", "Guide", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            DialogResult dr = (MessageBox.Show("YOU ARE ABOUT TO UNLOCK YOUR BOOTLOADER!\nYOU WILL VOID YOUR WARRENTY!  Do you wish to continue?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning));
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                Forms.Unlock_Bootloader unlock = new Forms.Unlock_Bootloader();
                unlock.Show();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool EnableGuide = Properties.Settings.Default.EnableGuide;
            if (EnableGuide == true)
            {
                MessageBox.Show("This will delete every file on your system partition", "Guide", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            DialogResult results = MessageBox.Show("ERASE SYSTEM?", "ARE YOU SURE?", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (results == DialogResult.Yes)
            {
                try
                {
                    Process.Start(Application.StartupPath + @"\rebootFastboot.bat");

                    string strCmdText;
                    strCmdText = "erase system";
                    System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", strCmdText);

                }
                catch (Exception objException)
                {
                    MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            bool EnableGuide = Properties.Settings.Default.EnableGuide;
            if (EnableGuide == true)
            {
                MessageBox.Show("This will delete every file on your boot partition", "Guide", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            DialogResult results = MessageBox.Show("ERASE BOOT?", "ARE YOU SURE?", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (results == DialogResult.Yes)
            {
                try
                {
                    Process.Start(Application.StartupPath + @"\rebootFastboot.bat");

                    string strCmdText;
                    strCmdText = "erase boot";
                    System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", strCmdText);

                }
                catch (Exception objException)
                {
                    MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            bool EnableGuide = Properties.Settings.Default.EnableGuide;
            if (EnableGuide == true)
            {
                MessageBox.Show("This will delete every file on your data and cache partition", "Guide", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            DialogResult results = MessageBox.Show("ERASE ALL USER DATA?", "ARE YOU SURE?", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (results == DialogResult.Yes)
            {
                try
                {
                    Process.Start(Application.StartupPath + @"\rebootFastboot.bat");

                    string strCmdText;
                    strCmdText = "-w";
                    System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", strCmdText);

                }
                catch (Exception objException)
                {
                    MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            bool EnableGuide = Properties.Settings.Default.EnableGuide;
            if (EnableGuide == true)
            {
                MessageBox.Show("This will replace the boot partition from the given image file (boot.img/sin)", "Guide", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            string results2string;
            DialogResult results2 = KernelPath.ShowDialog();
            if (results2 == DialogResult.OK)
            {
                results2string = KernelPath.FileName;

                DialogResult results = MessageBox.Show("OVERWRITE BOOT?", "ARE YOU SURE?", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
                if (results == DialogResult.Yes)
                {
                    try
                    {
                        Process.Start(Application.StartupPath + @"\rebootFastboot.bat");

                        string strCmdText = "flash boot "+results2string;

                        System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", strCmdText);

                    }
                    catch (Exception objException)
                    {
                        MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
        private void button10_Click(object sender, EventArgs e)
        {
            bool EnableGuide = Properties.Settings.Default.EnableGuide;
            if (EnableGuide == true)
            {
                MessageBox.Show("This will replace the system partition from the given image file (system.sin/img)", "Guide", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
             string results2string;
            DialogResult results2 = SystemPath.ShowDialog();
            if (results2 == DialogResult.OK)
            {
                results2string = SystemPath.FileName;

                DialogResult results = MessageBox.Show("OVERWRITE SYSTEM?", "ARE YOU SURE?", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
                if (results == DialogResult.Yes)
                {
                    try
                    {
                        Process.Start(Application.StartupPath + @"\rebootFastboot.bat");

                        string strCmdText;
                        strCmdText = "flash system "+results2string;
                        System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", strCmdText);

                    }
                    catch (Exception objException)
                    {
                        MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        

        private void button12_Click(object sender, EventArgs e)
        {
            Forms.Relock_Bootloader proc = new Forms.Relock_Bootloader();
            proc.Show();
            this.Hide();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            string results3string;
            DialogResult results3 = KernelPath.ShowDialog();
            if (results3 == DialogResult.OK)
            {
                results3string = SystemPath.FileName;
                try
                {
                    Process.Start(Application.StartupPath + @"\rebootFastboot.bat");

                    string strCmdText;
                    strCmdText = "boot " + results3string;
                    System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", strCmdText);

                }
                catch (Exception objException)
                {
                    MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            string results3string;
            DialogResult results3 = APKPath.ShowDialog();
            if (results3 == DialogResult.OK)
            {
                results3string = APKPath.FileName;
                if (results3string.Contains(" "))
                {
                    MessageBox.Show("Can't use path with spaces", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    try
                    {
                        string strCmdText;
                        strCmdText = "install " + results3string;
                        System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\adb.exe", strCmdText);

                    }
                    catch (Exception objException)
                    {
                        MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\fastboot.exe", "reboot system");

                System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\adb.exe", "reboot");

            }
            catch (Exception objException)
            {
                MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\adb.exe", "reboot safemod");

            }
            catch (Exception objException)
            {
                MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start(Application.StartupPath + @"\BIN\adb.exe", "reboot bootloader");

            }
            catch (Exception objException)
            {
                MessageBox.Show("Error: " + objException, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
